<?php

namespace app\admin\model;

use think\Model;


class Course extends Model
{

    

    

    // 表名
    protected $name = 'course';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'flag_text',
        'pro_json_list'
    ];
    

    
    public function getFlagList()
    {
        return ['normal' => __('Flag normal'), 'hot' => __('Flag hot'), 'recommend' => __('Flag recommend')];
    }


    public function getFlagTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['flag']) ? $data['flag'] : '');
        $list = $this->getFlagList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public function getProJsonListAttr($value, $data)
    {
        if(isset($data['pro_json'])){
            return json_decode($data['pro_json'],true);
        }else{
            return [];
        }
        $list = $this->getFlagList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function video()
    {
        return $this->belongsTo('Video', 'st_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public static function playTree($id,$user_id=0)
    {
        $cpackage=Cpackage::where('course_id',$id)->select();
        $packages=Package::where('id','in',array_column($cpackage,'package_id'))->order('weigh desc,id asc')->select();

        return $packages;
    }
    public static function myCourse($user_id){
        $order=Order::with(['course'])->where('user_id',$user_id)->where('status','paid')->where('exit_time','>',time())->select();
        return $order;
    }
}
